package com.han.hotelplat.cms.controller.systemuserhotel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.JsonArray;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.ResponseWrapTo.CodeType;
import com.han.hotelplat.comm.base.Pager;
import com.han.hotelplat.comm.utils.RequestUtil;
import com.han.hotelplat.pojo.systemuser.Systemuser;
import com.han.hotelplat.pojo.sysuserhotel.SysUserHotel;
import com.han.hotelplat.service.systemuser.SystemuserService;
import com.han.hotelplat.service.sysuserhotel.SysUserHotelService;

@Controller
@RequestMapping("/cms/sysuserhotel")
public class CMSSysUserHotelController {
	@Autowired
	private SysUserHotelService sysUserHotelService;
	@Autowired
	private SystemuserService systemuserService;
	
	/**
	 * 是否绑定了所有酒店
		 * @description	
		 * @author  han1088
		 * @param userId
		 * @param areaCode
		 * @param price
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "isBindAllHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String isBindAllHotel(String userId){
		boolean re = sysUserHotelService.isBindAllHotel(userId);
		if(re){
			return new ResponseWrapTo(CodeType.success, "数据获取成功", 1).toJson();
		}
		return new ResponseWrapTo(CodeType.success, "数据获取成功", 0).toJson();
	}
	
	
	/**
	 * 获取该用户已绑定的酒店
		 * @description	
		 * @author  han1088
		 * @param userId
		 * @param areaCode
		 * @param price
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getBindHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String getBindHotel(String userId, Integer areaCode, String price, Integer pageNo){
		if(StringUtils.isBlank(userId)){
			return new ResponseWrapTo(-1, "用户ID不能为空", null).toJson();
		}
		Pager<SysUserHotel> data = sysUserHotelService.getBindHotelByPage(userId, areaCode, price,pageNo);
		return new ResponseWrapTo(CodeType.success, "数据获取成功", data).toJson();
	}
	
	
	/**
	 * 获取所有酒店
		 * @description	
		 * @author  han1088
		 * @param userId
		 * @param areaCode
		 * @param price
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getAllHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String getAllHotel(String userId, Integer areaCode, String price, Integer pageNo){
		if(StringUtils.isBlank(userId)){
			return new ResponseWrapTo(-1, "用户ID不能为空", null).toJson();
		}
		Pager<SysUserHotel> data = sysUserHotelService.getAllHotelByPage(userId, areaCode, price,pageNo);
		return new ResponseWrapTo(CodeType.success, "数据获取成功", data).toJson();
	}
	
	//绑定酒店
	@ResponseBody
	@RequestMapping(value = "bindingHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String bindingHotel(String userId, String hotelIds, Integer bindAll){
		JSONArray hids = JSONArray.parseArray(hotelIds);
		if(bindAll != null && bindAll.intValue() == 1){//绑定所有酒店
			Systemuser user = (Systemuser) RequestUtil.getSessionAttribute(Constant.CMS_SESSION_LOGIN_USER);
			user = systemuserService.findUserByUserid(userId);
			user.setBindAllHotel(1);//标记为绑定所有酒店
			systemuserService.update(user);
		}
		sysUserHotelService.bindingHotel(userId, hids, bindAll);
		return new ResponseWrapTo(CodeType.success, "绑定成功", null).toJson();
	}
	
	//解绑酒店
	@ResponseBody
	@RequestMapping(value = "unbindHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String unbindHotel(String userId, String ids, Integer unBindAll){
		JSONArray id = JSONArray.parseArray(ids);
		sysUserHotelService.unbindHotel(id,unBindAll,userId);
		return new ResponseWrapTo(CodeType.success, "解绑成功", null).toJson();
	}
}
